package com.tj.jinzao.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tj.jinzao.R;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.MessageBean;
import com.tj.jinzao.model.MessageLiveComment;
import com.tj.jinzao.utils.ToastUtils;

import java.util.List;



public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {

    private List<MessageBean> messageBeanList;
    protected final LayoutInflater inflater;
    private Context context;

    public MessageAdapter(Context context, List<MessageBean> messageBeanList) {
        inflater = ((Activity) context).getLayoutInflater();
        this.context=context;
        this.messageBeanList = messageBeanList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_message_live, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        setupView(holder, position);
    }

    @Override
    public int getItemCount() {
        return messageBeanList.size();
    }


    private void setupView(MyViewHolder holder, int position) {

        MessageBean bean = messageBeanList.get(position);
        try {
            MessageLiveComment jsonObject= JSON.parseObject(bean.getMessage(), MessageLiveComment.class);
            holder.content.setText(jsonObject.getContent());
            if(jsonObject.getType().equals("2")){
                holder.nickName.setVisibility(View.VISIBLE);
                holder.nickName.setTextColor(context.getResources().getColor(R.color.live_blue));
                if(jsonObject.getUserId().equals(String.valueOf(UserManager.getInstance().getUser().getId()))){
                    holder.nickName.setText("我：");
                }else {
                    holder.nickName.setText(jsonObject.getNickName()+"：");
                }
                holder.content.setTextColor(context.getResources().getColor(R.color.global_bg));
                holder.content_lay.setBackgroundResource(R.drawable.live_message_white);
            }else if(jsonObject.getType().equals("3")){
                holder.nickName.setVisibility(View.GONE);
                holder.content.setTextColor(context.getResources().getColor(R.color.white));
                holder.content_lay.setBackgroundResource(R.drawable.live_message_blue);
            }else{
                holder.nickName.setVisibility(View.VISIBLE);
                if(jsonObject.getUserId().equals(String.valueOf(UserManager.getInstance().getUser().getId()))){
                    holder.nickName.setText("我：");
                    holder.nickName.setTextColor(context.getResources().getColor(R.color.live_blue));
                }else {
                    holder.nickName.setText(jsonObject.getNickName()+"：");
                    holder.nickName.setTextColor(context.getResources().getColor(R.color.global_bg));
                }
                holder.content.setTextColor(context.getResources().getColor(R.color.global_bg));
                holder.content_lay.setBackgroundResource(R.drawable.live_message_white);

            }

        }catch (Exception e){
            e.printStackTrace();
            holder.nickName.setVisibility(View.VISIBLE);
            holder.content.setText(bean.getMessage());
            holder.nickName.setText("xx：");
            holder.nickName.setTextColor(context.getResources().getColor(R.color.global_bg));
            holder.content.setTextColor(context.getResources().getColor(R.color.global_bg));
            holder.content_lay.setBackgroundResource(R.drawable.live_message_white);
        }
        holder.content_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickLinstener!=null){
                    ToastUtils.showToast(context,"666");
                    onItemClickLinstener.onItemClick();
                }
            }
        });

//        if (bean.isBeSelf()) {
//            holder.textViewSelfName.setText(bean.getAccount());
//            holder.textViewSelfMsg.setText(bean.getMessage());
//
//        } else {
//            holder.textViewOtherName.setText(bean.getAccount());
//            holder.textViewOtherMsg.setText(bean.getMessage());
//            if (bean.getBackground() != 0) {
//                holder.textViewOtherName.setBackgroundResource(bean.getBackground());
//            }
//        }
//
//        holder.layoutRight.setVisibility(bean.isBeSelf() ? View.VISIBLE : View.GONE);
//        holder.layoutLeft.setVisibility(bean.isBeSelf() ? View.GONE : View.VISIBLE);
    }
    public OnItemClickLinstener onItemClickLinstener;
    public void setOnItemClickLinstener(OnItemClickLinstener onItemClickLinstener){
        this.onItemClickLinstener=onItemClickLinstener;
    }
    public interface OnItemClickLinstener{
        void onItemClick();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView content,nickName;
        LinearLayout content_lay;

        public MyViewHolder(View itemView) {
            super(itemView);

            nickName = (TextView) itemView.findViewById(R.id.nickName);
            content = (TextView) itemView.findViewById(R.id.content);
            content_lay = (LinearLayout) itemView.findViewById(R.id.content_lay);
        }
    }
}
